package com.ruoyi.elebook.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.elebook.domain.BookClass;
import com.ruoyi.elebook.service.IBookClassService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类Controller
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@RestController
@RequestMapping("/bookclass")
public class BookClassController extends BaseController
{
    @Autowired
    private IBookClassService bookClassService;

    /**
     * 查询分类列表
     */
    @RequiresPermissions("elebook:bookclass:list")
    @GetMapping("/list")
    public AjaxResult list(BookClass bookClass)
    {
        List<BookClass> list = bookClassService.selectBookClassList(bookClass);
        return success(list);
    }


    /**
     * 查询部门列表（排除节点）
     */
    @RequiresPermissions("elebook:bookclass:list")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
    {
        List<BookClass> list = bookClassService.selectBookClassList(new BookClass());
        list.removeIf(d -> d.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
        return success(list);
    }
    /**
     * 导出分类列表
     */
    @RequiresPermissions("elebook:bookclass:export")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookClass bookClass)
    {
        List<BookClass> list = bookClassService.selectBookClassList(bookClass);
        ExcelUtil<BookClass> util = new ExcelUtil<BookClass>(BookClass.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @RequiresPermissions("elebook:bookclass:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bookClassService.selectBookClassById(id));
    }

    /**
     * 新增分类
     */
    @RequiresPermissions("elebook:bookclass:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookClass bookClass)
    {
        if (!bookClassService.checkBookClassNameUnique(bookClass))
        {
            return error("新增分类'" + bookClass.getName() + "'失败，分类名称已存在");
        }
        if(!bookClassService.checkBookClassCodeUnique(bookClass)){
            return error("新增代号'" + bookClass.getCode() + "'失败，分类代号已存在");
        }
        bookClass.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bookClassService.insertBookClass(bookClass));
    }

    /**
     * 修改分类
     */
    @RequiresPermissions("elebook:bookclass:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookClass bookClass)
    {
        Long deptId = bookClass.getId();
        if (!bookClassService.checkBookClassNameUnique(bookClass))
        {
            return error("修改分类'" + bookClass.getName() + "'失败，分类名称已存在");
        }
        if(!bookClassService.checkBookClassCodeUnique(bookClass)){
            return error("修改代号'" + bookClass.getCode() + "'失败，分类代号已存在");
        }
         if (bookClass.getParentId().equals(deptId))
        {
            return error("修改分类'" + bookClass.getName() + "'失败，上级分类不能是自己");
        }
        return toAjax(bookClassService.updateBookClass(bookClass));
    }

    /**
     * 删除分类
     */
    @RequiresPermissions("elebook:bookclass:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        if (bookClassService.hasChildByBookClassId(ids))
        {
            return warn("存在下级分类,不允许删除");
        }
        return toAjax(bookClassService.deleteBookClassById(ids));
    }
}
